package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.pojo.OrderDetail;
import com.itheima.pojo.Orders;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersConterllor {
    @Autowired
    private OrdersService ordersService;


    /*
     * 去支付，生成订单*/
    @PostMapping("/submit")
    public R<String> ordersGenerate(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");

    }

    /*
    * 分页查询订单*/
    @GetMapping("/userPage")
    public R<Page> selectPage(Integer page,Integer pageSize){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersService.page(ordersPage,wrapper);
        return R.success(ordersPage);
    }
    /*
     * 管理端订单分页*/
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number,String beginTime,String endTime){
        //构建分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage=new Page<>();
        //构建条件查询器
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据number进行模糊查询
        lambdaQueryWrapper.like(!StringUtils.isEmpty(number),Orders::getNumber,number);
//        log.info("开始时间：{}",beginTime);
//        log.info("结束时间：{}",endTime);
        //根据Datetime进行时间范围查询
        if (beginTime!=null&&endTime!=null){
            lambdaQueryWrapper.ge(Orders::getOrderTime,beginTime);
            lambdaQueryWrapper.le(Orders ::getOrderTime,endTime);
        }
        //添加排序条件，根据时间
        lambdaQueryWrapper.orderByDesc(Orders ::getOrderTime);

        //对于订单来说是很多组，对其实现分页展示在页面上，便于查看
        ordersService.page(pageInfo,lambdaQueryWrapper);
        //实现对象拷贝
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        //使用stream流来向订单表中添加
        List<OrdersDto> list = records.stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            String name = "用户"+item.getUserId();
            ordersDto.setUserName(name);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }
    /*
     * 修改订单操作*/
    @PutMapping
    public R<String> revise(@RequestBody Orders orders) {

        ordersService.updateById(orders);

        return R.success("操作成功");


    }
}
